package meihua.stopcarapp.widget.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StyleRes;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import meihua.stopcarapp.R;

/**
 * Name: LGZ
 * Time: 2017/5/17.
 * Dec:
 */

public class UserInfoDialog extends Dialog {

    private Context context;
    private OnUserinfoClickListener onUserinfoClickListener;
    String title = "title";
    String hint = "hint";
    String btn_str = "提交";

    public UserInfoDialog(@NonNull Context context) {
        super(context);
        this.context = context;
    }

    public UserInfoDialog(@NonNull Context context, @StyleRes int themeResId) {
        super(context, themeResId);
        this.context = context;
    }

    public UserInfoDialog(@NonNull Context context, @StyleRes int themeResId, String title, String hint) {
        super(context, themeResId);
        this.context = context;
        this.title = title;
        this.hint = hint;
    }
    public UserInfoDialog(@NonNull Context context, @StyleRes int themeResId, String title, String hint, String btn) {
        super(context, themeResId);
        this.context = context;
        this.title = title;
        this.hint = hint;
        this.btn_str = btn;
    }

    protected UserInfoDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        this.context = context;
    }

    public OnUserinfoClickListener getOnUserinfoClickListener() {
        return onUserinfoClickListener;
    }

    public void setOnUserinfoClickListener(OnUserinfoClickListener onUserinfoClickListener) {
        this.onUserinfoClickListener = onUserinfoClickListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    public EditText getEditText() {
        return editText;
    }

    private TextView btnSubmit,btnCancle;
    private EditText editText;
    private void init() {
        //以view的方式引入，然后回调activity方法，setContentView，实现自定义布局
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_userinfo, null);
        setContentView(view);
//        setCancelable(false);
        ((TextView)view.findViewById(R.id.dialog_userinfo_tv)).setText(title);
        editText = (EditText)view.findViewById(R.id.dialog_userinfo_et);
        editText.setHint(hint);

        btnCancle = (TextView) view.findViewById(R.id.dialog_userinfo_can);
        btnSubmit = (TextView) view.findViewById(R.id.dialog_userinfo_sub);
        btnSubmit.setText(btn_str);
        btnCancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onUserinfoClickListener.cancleClick();
            }
        });
        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onUserinfoClickListener.submitClick();
            }
        });
        //设置dialog大小，这里是一个小赠送，模块好的控件大小设置
        Window dialogWindow = getWindow();
        WindowManager manager = ((Activity) context).getWindowManager();
        WindowManager.LayoutParams params = dialogWindow.getAttributes(); // 获取对话框当前的参数值
        dialogWindow.setGravity(Gravity.CENTER);//设置对话框位置
        Display d = manager.getDefaultDisplay(); // 获取屏幕宽、高度
        params.width = (int) (d.getWidth() * 0.7); // 宽度设置为屏幕的0.65，根据实际情况调整
        dialogWindow.setAttributes(params);
    }

    public interface OnUserinfoClickListener{
        void submitClick();
        void cancleClick();
    }

}
